package com.maotai.llzhou.timetask;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.service.MaotaiService;
import com.maotai.llzhou.thread.MaotaiYuYueCheckThreadTemp;
import com.maotai.llzhou.util.IPFactory;

/**
 * 登录并且抢购
 */
public class MaotaiYuYueCheckTaskTemp {

	@Resource(name = "maotaiService")
	private MaotaiService maotaiService;

	public void beginTask() {
		try {
			List<Map<String, Object>> list = maotaiService
					.getZhanghaoYuYueCheckTemp();
			if (list != null && list.size() > 0) {
				JSONArray jArray = IPFactory.getIpAndPort();
				int count = list.size();
				if(jArray!=null&&jArray.size()<count){
					count=jArray.size();
				}
				for(int i=0;i<count;i++){
					Map<String, Object> zhanghao = list.get(i);
					JSONObject ip = (JSONObject)jArray.get(i);
					String ipAndPort = ip.getString("ip")+":"+ip.getString("port");
					zhanghao.put("yuyuecheckstatus", "1");
					maotaiService.updateZhanghaoYuYueCheckStatusTemp(zhanghao);
					MaotaiYuYueCheckThreadTemp mcl = new MaotaiYuYueCheckThreadTemp(maotaiService, zhanghao,ipAndPort);
					mcl.start();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
